package anno.thrift.module;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.Getter;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class BaseModule {
    /**
     * FormInput信息
     */
    @Getter
    private HashMap<String, String> input;

    /**
     * 是否通过了授权
     */
    private boolean authorized = false;

    public boolean isAuthorized() {
        return authorized;
    }

    /**
     * 前置初始化方法
     * @param input
     * @return
     */
    public  boolean Init(HashMap<String, String> _input)
    {
        this.input = _input;

        if (input != null && input.containsKey("body")){
            String objstr = input.get("body");
            try {
                objstr =  URLDecoder.decode(objstr, "utf-8");
                JSONObject jsobj = JSON.parseObject(objstr);
                if(jsobj!=null ){
                    Iterator iter = jsobj.entrySet().iterator();
                    while (iter.hasNext()) {
                        Map.Entry entry = (Map.Entry) iter.next();
                       //System.out.println(entry.getKey().toString());
                        //System.out.println(entry.getValue().toString());
                        input.put(entry.getKey().toString(), entry.getValue().toString());
                    }
                }
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        return true;
    }
}
